//
//  MK_NetWorking.m
//  LoveBeen
//
//  Created by 杨尚达 on 2017/5/14.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_NetWorking.h"
#import <AFNetworking.h>



@interface MK_NetWorking ()

///HTTP请求manager
@property(nonatomic,strong)AFHTTPSessionManager* manager;

@property(nonatomic,assign)NSInteger changeNumber;

@end

@implementation MK_NetWorking




///单例方法
+(instancetype)sharedNetWorking{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance=[self new];
    });
    return instance;
}
///初始化方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        self.manager=[[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];
        //2.可接受格式
        self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //3.请求格式（默认：二进制）
        self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //4.超时
        self.manager.requestSerializer.timeoutInterval = 10.0;
        self.isConnection=YES;
        
    }
    return self;
}

///普通的GET HTTP请求
- (NSURLSessionDataTask *)Miku_GET:(NSString *)URLString
                        parameters:(id)parameters
                          progress:(void (^)(NSProgress * _Nonnull progress))downloadProgress
                           success:(void (^)(NSURLSessionDataTask * _Nonnull dataTask, id _Nullable responseObject))success
                           failure:(void (^)(NSURLSessionDataTask * _Nullable dataTask, NSError * _Nonnull error))failure{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    if (self.isConnection) {
        return [self.manager GET:URLString parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            success(task,responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            failure(task,error);
        }];
    }else{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        return nil;
    }
    
    
}

///普通的POST请求
- (NSURLSessionDataTask *)Miku_POST:(NSString *)URLString
                         parameters:(id)parameters
                           progress:(void (^)(NSProgress * _Nonnull progress))uploadProgress
                            success:(void (^)(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject))success
                            failure:(void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure
{
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    if (self.isConnection) {
        return [self.manager POST:URLString parameters:parameters progress:uploadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            success(task,responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            failure(task,error);
        }];
    }else{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        return nil;
    }
}


@end
